/* 
 * Created on 14. August 2008, 11:41
 *
 *      Copyright 2008 Oliver Eichner <o.eichner@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *
 *		Parts included from HDM
 *		hdm.c - Hard Disk Metadata
 *		Copyright (C) Finnbarr P. Murphy 2010  <fpm[AT]fpmurphy.com>
 *		Read more: http://blog.fpmurphy.com/2010/05/hard-disk-metadata.html#ixzz16WBhY72R
 */

/**
 * @file Device.h
 * Device Class header
 */

#ifndef _DEVICE_H
#define	_DEVICE_H

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>


#include <string.h>
#include <string>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <linux/fs.h>
#include <asm/byteorder.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <parted/parted.h>

#define DUMPTXT          2
#define NONEWLINE        0

#define DI_VERSION       "1.0"
#define TRANSPORT_MAJOR   0xDE
#define TRANSPORT_MINOR   0xDF
#define ATA_PIDENTIFY     0xA1
#define ATA_IDENTIFY      0xEC
#define NMRR              0xD9
#define CAPAB             0x31
#define CMDS_SUPP_1       0x53
#define VALID             0xC000
#define VALID_VAL         0x4000
#define SUPPORT_48_BIT    0x0400
#define LBA_SUP           0x0200
#define LBA_LSB           0x64
#define LBA_MID           0x65
#define LBA_48_MSB        0x66
#define LBA_64_MSB        0x67


using namespace std;

/**
 * @class Device
 * The Device class, one object per device wich stores tableau-parm informations..
 * @author Oliver Eichner
 * @version 0.3
 */
class Device {
public:
    Device(string devName);
    
    ~Device();

    virtual void setComment(string nComment);

    virtual string getComment();
    string getDeviceName();
    string getVendor();
    string getModel();
    string getSerial();
    string getSize();
    int getSizeMB();
    string getSizeBLK();
    
    //tableau-parm getter
     //## Bridge Information ##
    string getBridge_chan_index();
    string getBridge_chan_type();
    bool getBridge_writes_permitted();
    bool getBridge_declare_write_blocked();
    bool getBridge_declare_write_errors();
    string getBridge_serial();
    string getBridge_vendor();
    string getBridge_model();
    string getBridge_firmware_date();
    string getBridge_firmware_time();
    // ## Drive Information ##
    string getBridge_drive_vendor();
    string getBridge_drive_model();
    string getBridge_drive_serial();
    string getBridge_drive_revision();

    //## Drive HPA/DCO/Security Information ##
    bool getBridge_security_in_use();
    bool getBridge_security_support();
    bool getBridge_hpa_in_use();
    bool getBridge_hpa_support();
    bool getBridge_dco_in_use();
    bool getBridge_dco_support();
    string getBridge_drive_capacity();
    string getBridge_hpa_capacity();
    string getBridge_dco_capacity();
    string getBridge_dco_challenge_key();

private:
    string comment;
    string deviceName;
    string vendor;
    string model;
    string serial;
    string size, capacity;
    
    string bus; // atm not in use

	__u16 *id;
	struct hd_geometry *g;
	int fd;

    //tableau-parm vars
    //## Bridge Information ##
    string bridge_chan_index;
    string bridge_chan_type;
    bool bridge_writes_permitted;
    bool bridge_declare_write_blocked;
    bool bridge_declare_write_errors;
    string bridge_serial;
    string bridge_vendor;
    string bridge_model;
    string bridge_firmware_date;
    string bridge_firmware_time;
    // ## Drive Information ##
    string bridge_drive_vendor;
    string bridge_drive_model;
    string bridge_drive_serial;
    string bridge_drive_revision;

    //## Drive HPA/DCO/Security Information ##
    bool bridge_security_in_use;
    bool bridge_security_support;
    bool bridge_hpa_in_use;
    bool bridge_hpa_support;
    bool bridge_dco_in_use;
    bool bridge_dco_support;
    string bridge_drive_capacity;
    string bridge_hpa_capacity;
    string bridge_dco_capacity;
    string bridge_dco_challenge_key;


    long long sizeBlk;
    string sizeBlkString;
    int sizeMB;
    void fetchSys();
    void fetchTableauParmStats();

	//hd meta data
	struct hd_geometry* get_geometry(int fd);
	void* get_diskinfo(int fd);
	char* get_transport(__u16 id[]);
	char* get_rpm(__u16 id[]);
	char* ascii_string(__u16 *p, unsigned int len);
	char* get_capacity(int fd, __u16 id[]);
	void dump_partitions(char *device, int dumpmode, int nlmode);
	void dump(char *device);
};

#endif	/* _DEVICE_H */

